package com.pokermind.repository.mapper;

import com.pokermind.model.po.PokerUser;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * PokerUser 行映射器
 * 映射最新的简化用户表结构
 * 
 * @author PokerMind Team
 */
public class PokerUserRowMapper implements RowMapper<PokerUser> {
    
    @Override
    public PokerUser mapRow(ResultSet rs, int rowNum) throws SQLException {
        PokerUser user = new PokerUser();
        
        // 基本字段
        user.setId(rs.getLong("id"));
        user.setPlayerId(rs.getString("player_id"));
        user.setPlayerName(rs.getString("player_name"));
        
        // 统计数据
        user.setTotalHands(getIntegerOrNull(rs, "total_hands"));
        user.setTotalProfit(rs.getBigDecimal("total_profit"));
        
        // 5个核心行为指标
        user.setVpipRate(rs.getBigDecimal("vpip_rate"));
        user.setAggressionLevel(rs.getString("aggression_level"));
        user.setWinRate(rs.getBigDecimal("win_rate"));
        user.setPlayingStyle(rs.getString("playing_style"));
        user.setRecentForm(rs.getString("recent_form"));
        
        // 时间字段 - 使用Long时间戳
        user.setCreateTime(getLongOrNull(rs, "create_time"));
        user.setUpdateTime(getLongOrNull(rs, "update_time"));
        
        return user;
    }
    
    /**
     * 安全获取Integer值，处理NULL情况
     */
    private Integer getIntegerOrNull(ResultSet rs, String columnName) throws SQLException {
        int value = rs.getInt(columnName);
        return rs.wasNull() ? null : value;
    }
    
    /**
     * 安全获取Long值，处理NULL情况
     */
    private Long getLongOrNull(ResultSet rs, String columnName) throws SQLException {
        long value = rs.getLong(columnName);
        return rs.wasNull() ? null : value;
    }
}